package com.imy.common.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Locale;

public class DeviceUtils {

  /**
   * 获取手机IMEI码
   */
  @SuppressLint("MissingPermission")
  public static String getIMEI(Context context) {
    String imei = "";
    try {
      TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
      imei = tm.getDeviceId();
    } catch (Exception e) {
    }
    if (imei == null) {
      imei = "";
    }
    return imei;
  }

  public static String getMacAddress(Context context) {
    String mac="";
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
      mac = getMacDefault(context);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
      mac = getMacAddress();
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      mac = getMacFromHardware();
    }
    return mac;

  }

  public static String getMacFromHardware() {
    try {
      Enumeration nis = NetworkInterface.getNetworkInterfaces();
      if (nis == null) {
        return "";
      }

      while (nis.hasMoreElements()) {
        NetworkInterface ni = (NetworkInterface) nis.nextElement();

        byte[] hardwareAddress = ni.getHardwareAddress();
        if (hardwareAddress==null){
          return "";
        }
        String s = HexUtils.toHexString(hardwareAddress);
        return s;
      }
    } catch (SocketException e) {
      e.printStackTrace();
    }
    return "";
  }

  public static String getMacAddress() {
    String macSerial = null;
    String str = "";

    try {
      Process pp = Runtime.getRuntime().exec("cat/sys/class/net/wlan0/address");
      InputStreamReader ir = new InputStreamReader(pp.getInputStream());
      LineNumberReader input = new LineNumberReader(ir);

      while (null != str) {
        str = input.readLine();
        if (str != null) {
          macSerial = str.trim();//去空格
          break;
        }
      }
    } catch (IOException ex) {
      // 赋予默认值
      ex.printStackTrace();
    }

    return macSerial;
  }

  public static String getMacDefault(Context context) {
    String mac = "";
    if (context == null) {
      return mac;
    }
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = null;
    try {
      info = wifi.getConnectionInfo();
    } catch (Exception e) {
      e.printStackTrace();
    }

    if (info == null) {
      return null;
    }
    mac = info.getMacAddress();
    if (!TextUtils.isEmpty(mac)) {
      mac = mac.toUpperCase(Locale.ENGLISH);
    }
    return mac;
  }
}
